package com.pos;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.pos.modules.bean.OrderBean;
import com.pos.net.base.Request;
import com.pos.net.netparam.NetParams;
import com.pos.net.netparam.RequestUrls;
import com.pos.net.request.JsonNetRequest;
import com.pos.net.request.interfa.BaseRequestListener;
import com.pos.net.request.interfa.JsonRequestListener;

import org.json.JSONObject;

import java.util.List;

public class OrderService extends Service {
    public JsonNetRequest netRequest;
    List<OrderBean> datas;

    /*private Context context;


    public  OrderService(Context context){
        super();
        this.context=context;

    }*/
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.e("TAG", "onCreate: " + "启动服务");
        super.onCreate();
        netRequest = JsonNetRequest.getInstance(this);
        netRequest.setMainContext(this);

      /*  new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
            //
                    datas = YourApplication.orderDatabaseHelper.queryData();
                    if (datas.size() != 0) {
                      //  Log.e("TAGdddd", "run: "+datas.size() );
                        for (int i = 0; i < datas.size(); i++) {
                            Log.e("TAG5555", "run: "+"11111111111" );
                        *//*    netRequest.startRequest(RequestUrls.hostUrl + RequestUrls.posPayOrderSuccess,
                                    Request.Method.POST,
                                    NetParams.posPayOrderSuccess(OrderService.this, datas.get(i).getOrder_id(), 0, datas.get(i).getPayMethod(), datas.get(i).getPayBillNumber(),
                                            datas.get(i).getPayDate(), datas.get(i).getPayAmount(), datas.get(i).getCashAmount(), datas.get(i).getCashChange(), datas.get(i).getMemberPayToken(), datas.get(i).getSecondPayNumber(), datas.get(i).getMerCode(),
                                            datas.get(i).getMerName(), datas.get(i).getTermCode(), datas.get(i).getBankName(), datas.get(i).getBankCard(), datas.get(i).getBankCardExpired(), datas.get(i).getAuthorCode(),
                                            datas.get(i).getTradeBatchCode(),datas.get(i).getOrderCode()),
                                    i, listener);*//*
                          try {
                                Thread.sleep(60000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                        }

                    }

                }
            }
        }).start();*/



    }

    private BaseRequestListener listener = new JsonRequestListener() {

        @Override
        public void onStart(int requestId) {
        }

        @Override
        public void onError(int requestId, int errorCode, String errorMsg) {

        }

        @Override
        public void onSuccess(int requestId, String url, JSONObject jsonResult) {
        Log.e("TAG1111", "onSuccess: " + jsonResult.toString());
            Log.e("TAG1111", "onSuccess: " + datas.get(requestId).getOrder_id());
            Log.e("TAG1111", "onSuccess: " + datas.size());
           /*     Log.e("TAG", "onSuccess11111111: " +requestId);
            Log.e("TAG222222222", "onSuccess: "+requestId );
            Log.e("TAG33333333333", "onSuccess: "+datas.size() );*/
            YourApplication.orderDatabaseHelper.deleid(datas.get(requestId).getOrder_id());
            int retCode = jsonResult.optInt("retCode", 0);
           /* if(requestId+1==datas.size()){
                Log.e("TAG44444", "onSuccess: "+"执行" );
               // datas = YourApplication.orderDatabaseHelper.queryData();
            }
*/
            if (retCode == 1) {
                return;
            } else if (retCode == 2) {
                return;
            } else if (retCode == 3) {
                return;
            } else {


            }
             /*   String memberCode = jsonResult.optString("memberCode");
                String memberName = jsonResult.optString("memberName");
                String memberLevel = jsonResult.optString("memberLevel");
                String memberScore = jsonResult.optString("memberScore");
                String menberBalance = jsonResult.optString("menberBalance");
                String printInfo = jsonResult.optString("printInfo");

                Intent intent = new Intent(OrderService.this, PaySuccessActivity.class);
                intent.putExtra("memberCode", memberCode);
                intent.putExtra("memberName", memberName);
                intent.putExtra("memberLevel", memberLevel);
                intent.putExtra("memberScore", memberScore);
                intent.putExtra("printInfo", printInfo);
                intent.putExtra("totalPrice", finalPrice);
                intent.putExtra("payBillNumber",jsonResult.optString("orderCode"));
                startActivityForResult(intent, 0);*/
            Log.e("TAG1111", "onSuccess: " + datas.size());
        }
    };

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
